<%

# $Revision: 624 $

# FIXME Place named anchors in such a way that jumping to one does not crop the top part of the <th>.
# TODO Try out PagePacker and adjust print styles accordingly, http://weblog.bignerdranch.com/?p=23
# TODO Maybe remove project column from printed list views.
# TODO Parse match[:content] and create txmt:// links to wiki pages if they exist.

# [2007-06-30: REVISION 624]
# [FIXED] Setting contexts inactive should work now.
# [CHANGED] Project names are now hidden when the list view is printed.
# [2007-06-08: REVISION 623]
# [NEW] Tasks in inactive contexts are omitted from the list views. -- Inactive contexts are all those, which have their names added to "excluded.gtd"... entries present in "excluded.gtd", but prefixed with "#" are NOT treated at inactive, so you create blocks of contexts in "excluded.gtd" and easily enable or disable them.
# [NEW] Links contained in task descriptions are now clickable in the list views.

# ----------------------------------------------------------------------------

require 'digest/md5'

excluded_contexts = []
File.readlines(File.join(ENV['TM_DIRECTORY'], 'excluded.gtd')).each do |line|
  next if line.match(/^\s*#/) or line.match(/^\s*$/)
  excluded_contexts << line if !File.exist?(File.join(ENV['TM_DIRECTORY'], line.strip!))
end

link_pattern = Regexp.new('(((https?|s?ftp|ftps|file|smb|afp|nfs|(x-)?man|gopher|x-yojimbo-item|txmt)://|mailto:)[-:@a-zA-Z0-9_.~%+/?=&#]+)')
wiki_pattern = Regexp.new('([A-Z][a-z]+([A-Z][a-z]*)+)')

no_actions = true
$tags.each do |tag|
  tag[:label].strip!
  tag[:excluded] = excluded_contexts.include?(tag[:label])
  tag[:hashed_label]  = 'x' + Digest::MD5.hexdigest(tag[:label])
  tag[:escaped_label] = html_escape(tag[:label]).strip
  unless tag[:matches].empty? or tag[:excluded]
    no_actions = false
    count = 0
    tag[:matches].each do |match|
      match[:class] = (((count+=1) % 2) == 0) ? 'even' : 'odd'
      match[:escaped_file] = html_escape(File.basename(match[:file])).strip
      match[:escaped_content] = html_escape(match[:content]).strip
      match[:escaped_content].gsub!(link_pattern, '<a href="\1">\1</a>')
    end
  end
end

%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>GTD2 List</title>
    <style type="text/css" media="all">
        <% $tags.each do |tag| %>
            .<%= tag[:hashed_label] %> {
                background-color: #<%= tag[:color].strip %>;
            }
        <% end %>
    </style>
    <style type="text/css" media="screen">
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #bdbdbd;
            color: #f9f9f9;
        }

        body,
        th,
        td {
            font: 8pt 'lucida grande', sans-serif;
        }

        h1,
        h2,
        #timestamp {
            display: none;
        }

        table {
            clear: both;
            width: 100%;
            background-color: #202020;
            padding-bottom: 100%;
        }

        tr.odd {
            background-color: #272727;
        }

        tr.odd span {
            border: 1px solid #171717;
        }

        tr.even {
            background-color: #171717;
        }

        tr.even span {
            border: 1px solid #000;
        }

        th {
            border: solid #525252;
            border-width: 1px 0;
            background-color: #3a3a3a;
            padding: 0.3em 1em;
            font-weight: normal;
            text-align: left;
            text-transform: lowercase;
            letter-spacing: 0.25em;
            text-shadow: #171717 2px 2px 0;
        }

        th a {
            float: right;
        }

        th a:hover {
            text-decoration: none;
        }

        td {
            padding: 0.3em 1em 0.3em 2em;
            text-align: left;
            vertical-align: baseline;
            white-space: nowrap;
            border-bottom: 1px solid #0f0f0f;
        }

        td + td {
            padding-right: 0;
        }

        td + td + td {
            width: 100%;
            padding-left: 0.25em;
            padding-right: 1.5em;
            white-space: normal;
        }

        span {
            display: inline-block;
            margin-top: 0.3em;
            margin-right: 0.5em;
            width: 0.5em;
            height: 0.5em;
            border: 1px solid #333;
            vertical-align: baseline;
        }

        a {
            color: #7a7a7a;
            text-decoration: none;
        }

        a:hover {
            color: #f9f9f9;
            text-decoration: underline;
        }

        a:active {
            color: #c03;
        }

        li {
            float: left;
            display: inline-block;
            margin: 0;
            width: 14em;
            overflow: hidden;
            border: solid #b0b0b0;
            border-width: 0 1px 1px 0;
            padding: 0.25em 0.5em 0.25em 1em;
            color: #8c8c8c;
            white-space: nowrap;
            text-transform: lowercase;
        }

        li:hover {
            background-color: #b0b0b0;
        }

        li a {
            color: #2f002f;
        }
    </style>
    <style type="text/css" media="print">
        body,
        th,
        td {
            font: 10pt 'helvetica neue', sans;
        }

        h1,
        h2,
        ul,
        th a {
            display: none;
        }

        #timestamp {
            margin-bottom: 2em;
            font-family: times, serif;
            font-style: italic;
            text-align: right;
        }

        table {
            width: 100%;
            border-top: 1px solid #999;
        }

        th {
            border-bottom: 1px solid #000;
            padding: 0.5em 1em;
            padding-left: 0.5em;
            color: #000;
            font-weight: bold;
            text-align: left;
            text-transform: lowercase;
            letter-spacing: 0.25em;
        }

        td {
			display: none;
            border-bottom: 1px solid #999;
            padding: 0.3em 1em 0.3em 2em;
            text-align: left;
            vertical-align: top;
            white-space: nowrap;
        }

        td + td {
			display: table-cell;
            padding-left: 0.5em;
            padding-right: 0;
        }

        td + td + td {
			display: table-cell;
            width: 100%;
            padding-left: 0.25em;
            padding-right: 1em;
            white-space: normal;
        }

        a {
            font-family: times, serif;
            color: #000;
            font-style: italic;
            text-decoration: none;
        }

        span {
            display: inline-block;
            margin-top: 0.3em;
            margin-right: 0.5em;
            width: 0.5em;
            height: 0.5em;
            border: 1px solid #999;
            vertical-align: baseline;
        }
    </style>
</head>

<body>

    <a name="top"></a>
    <h1>GTD2 List</h1>

    <h2>Contexts</h2>
    <ul>
        <% $tags.each do |tag| %>
            <% next if tag[:excluded] %>
            <% chunk = tag[:matches].empty? ? tag[:escaped_label] : "<a href=\"##{tag[:hashed_label]}\">#{tag[:escaped_label]}</a>" %>
            <li><span class="<%= tag[:hashed_label] %>">&nbsp;</span><%= chunk %> (<%= tag[:matches].length %>)</li>
        <% end %>
    </ul>

    <h2>Actions</h2>
    <div id="timestamp"><%= Time.now.strftime('%A, %B %d, %Y &ndash; %H:%M, %Z') %></div>
    <table cellspacing="0">
    <% if no_actions %>
        <tr><th>Wow, no unfinished tasks in any of your active projects and contexts!</th></tr>
    <% else no_actions %>
        <% $tags.each do |tag| %>
            <% unless tag[:matches].empty? or tag[:excluded] %>
                <tr>
                    <th colspan="3"><a name="<%= tag[:hashed_label] %>"></a><%= tag[:escaped_label] %>
                        (<%= tag[:matches].length %>)
                        <a href="#top">&#x21E7;</a></th>
                </tr>
                <% tag[:matches].each do |match| %>
                    <tr class="<%= match[:class] %>">
                        <td><a href="<%= TextMate.file_link(match[:file], match[:line]) %>"><%= match[:escaped_file] %>, #<%= match[:line] %></a></td>
                        <td><span class="<%= tag[:hashed_label] %>">&nbsp;</span></td>
                        <td><%= match[:escaped_content] %></td>
                    </tr>
                <% end %>
            <% end %>
        <% end %>
    <% end %>
    </table>

</body>
</html>
